﻿using System;
using System.Data;
using System.IO;
using FixEd;

namespace Delimited
    {
        public class DelimitedFile : IParser
        {
            String samplename = @"C:\HMS Sample File.txt";
            private readonly string _Delimiter;
            private readonly DataSet _FileContent;
            private readonly Guid _ID;
            private bool IsHeaderOnFirstLine { get; set; }
            private string FileName { get; set; }
            public int[] ColumnWidths { get; set; }

            public DelimitedFile()
            {
                Guid.TryParse("{3991C980-AD8B-4DC4-A1F2-B211A345FE8F}", out _ID);
                FileName = samplename;
                _FileContent = new DataSet();
                _Delimiter = "|";
                IsHeaderOnFirstLine = true;
            }

            public Guid ID
            {
                get
                {
                    return _ID;
                }
            }

            public string Name
            {
                get
                {
                    return "Delimited Files";
                }
            }

            public string Description
            {
                get
                {
                    return "Handles Delimited Files";
                }
            }

            private void Split()
            {
                StreamReader sr = new StreamReader(FileName);
                _FileContent.Tables.Add();
                string[] columns;
                columns = sr.ReadLine().Split(_Delimiter.ToCharArray());
                ColumnWidths = new int[columns.Length];
                //if the first line does not contain the headings, we'll just use numbers
                if (IsHeaderOnFirstLine)
                {
                    for (int i = 0; i < columns.Length; i++)
                    {
                        _FileContent.Tables[0].Columns.Add(columns[i]);
                        ColumnWidths[i] = 0;
                    }
                }
                else
                {
                    for (int i = 0; i < columns.Length; i++)
                    {
                        _FileContent.Tables[0].Columns.Add(i.ToString());
                        ColumnWidths[i] = columns[i].Length;
                    }
                    _FileContent.Tables[0].Rows.Add(columns);
                }

                string data = sr.ReadToEnd();
                string[] rows = data.Split("\r".ToCharArray());
                foreach (string r in rows)
                {
                    string[] items = r.Split(_Delimiter.ToCharArray());
                    for (int x = 0; x < items.Length; x++)
                    {
                        if (items[x].Length > ColumnWidths[x])
                        {
                            ColumnWidths[x] = items[x].Length;
                        }
                    }
                    //split the row at the delimiter
                    _FileContent.Tables[0].Rows.Add(items);
                }
            }

            public DataSet ParseFile()
            {
                Split();
                return _FileContent;
            }
            public void Save(DataSet ds, string filePath)
            {
                throw new NotImplementedException();
            }
        }
    }
